package com.yzc.controller;

import com.yzc.pojo.MenuBean;
import com.yzc.service.impl.MenuBeanServiceImpl;
import com.yzc.util.ResultData;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Marker;
import org.slf4j.MarkerFactory;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/menu")
@RequiredArgsConstructor
public class MenuController {

    private static final Marker LOG_MARKER = MarkerFactory.getMarker("LOG");
    final MenuBeanServiceImpl menuBeanService;

    /**
     * 获取用户数据
     *
     * @return
     */
    @RequestMapping("/loadMenuDateInfo")
    public List<MenuBean> loadAll() {

        log.info(LOG_MARKER, "loadMenuDateInfo");
        return menuBeanService.loadMenuDateInfo();
    }

    @RequestMapping("/loadMenuDateInfos")
    public List<MenuBean> selectInfos(@RequestParam("name") String name) {
        log.info(LOG_MARKER, "selectInfos");
        return menuBeanService.selectInfosByWhere(name);
    }

    @RequestMapping("/loadMenuDateById")
    public List<MenuBean> loadMenuDateById(@RequestParam("id") int id) {
        log.info(LOG_MARKER, "loadMenuDateById");
        return menuBeanService.loadMenuDateById(id);
    }

    @RequestMapping("/loadMenuList")
    public List<MenuBean> loadMenuList() {
        log.info(LOG_MARKER, "loadMenuList");
        return menuBeanService.loadMenuList();
    }

    /**
     * 修改用户数据
     *
     * @return
     */
    @RequestMapping("/updateMenuInfo")
    public Map updateInfo(@RequestBody MenuBean menuBean) {
        log.info(LOG_MARKER, "updateInfo");
        return menuBeanService.updateInfo(menuBean);
    }

    /**
     * 更新用户数据
     *
     * @return
     */
    @RequestMapping("/saveMenuInfo")
    public Map saveMenuInfo(@RequestBody MenuBean bean) {
        log.info(LOG_MARKER, "saveMenuInfo");
        //pass加密，可以，但是不合理
        return menuBeanService.saveInfo(bean);
    }

    /**
     * 删除用户数据
     *需要将t_menu和t_role_menu表进行逻辑删除
     * @return
     */
    @DeleteMapping("/deleteMenuInfo")
    public ResultData deleteInfo(@RequestParam("id") Integer id) {
        menuBeanService.deleteInfo(id);
        log.info(LOG_MARKER, "删除了一个菜单");
        return ResultData.success();
    }

    /**
     * 根据id查询
     *
     * @return
     */
    @RequestMapping("/getInfo")
    public MenuBean getInfo(@RequestParam("id") int id) {
        log.info(LOG_MARKER, "getInfo");
        return menuBeanService.getInfoById(id);
    }

    @RequestMapping("/loadMenuTree")
    public List<MenuBean> loadMenuTree() {
        log.info(LOG_MARKER, "loadMenuTree");
        return menuBeanService.loadMenuTree();
    }

    @PostMapping("/loadMenuTreeByUserid")
    public List<MenuBean> loadMenuTreeByUserid(@RequestParam("userid") int userid) {
        log.info(LOG_MARKER, "loadMenuTreeByUserid");
        return menuBeanService.loadMenuTreeByUserid(userid);
    }

}
